package com.ego.product.component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ego.product.vo.order.PayVo;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @program: ego
 * @description: 支付宝支付
 * @author: KamTang
 * @create: 2022-12-20 09:44
 **/
@ConfigurationProperties(prefix = "alipay")
@Component
@Getter
public class AlipayTemplate {

    /**
     * 在支付宝创建的应用的id
     */
    private final String appId = "2021000122602893";

    /**
     * 商户私钥，您的PKCS8格式RSA2私钥
     */
    private final String merchantPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCGTQPvC9Z3issriyXmNhz8w2Yk2F8N65wp0+Q6vNctoLWNvQAjJdZ83QEx3JG9rtrcecx6YUUCBHxvY5HrdtsRqqipzeQhphtiVG+gIlJokeMGdkX9fnisnkF/mXFoiDgkkFRrMNt6LY4zj8UAlXMejbjIMV0BDFXfAp38LC2IYwWFA03HCa9jvtCYMjdfxGJ1VfSxs6ztI2yMNKhERm8pdhTP1Ri+2GE6/pwJBKdwpuEue7WzOMYbzrI/OjJQeig192+XuU3enZrX+MRNauL++yBsgEdN/X8SaP0+EcLmZ2XE9j6qTOxFaqoZPQfENj9wcs0mGc8h30fhoqnzl4P5AgMBAAECggEAEOupIDgMmFkbSewpJT0SH+J6fw/nwfg6X6a7rY7XSX9jAG2ck9ETmf26Rc71acGa/4Y09gJFUIDCMMKJwvHhnfMzdtli8b5c2uGqgrFwUo523PyPyuVhTNZPiEUL3dCzyh+JE0w/isAsl2B4wJ8TtvjCHC7j+drVAtjwkLH8Np+baQ578dlPrDHrlEB0vQO5sQ0J2ZE81HPnNFIfs4VawT2kKkMNa6VXC4OxkgjU+lSDU9HoIfpDyVRfbFSX2342D7JPeo59PImSossdcoTI29sOAaHGcz03GwZWxNvb7vVi07hwjq+fGjnDbUKU84K70gF+X3trppfmaVh64fYR9QKBgQDlsfIklzpGmPKombEDm2CVM+pG/un65RLeiiy7T2bnDEOYdDW9vskok+VKfwIHBjM3h5iE8vtKI/96O9pHo9NR6EspcuQ+jNEQiWYFGbiLlVA16jQifwhtCwhTsVMjRkRYnOLNempVqmQzfy3cUokpbnigLuFxp+nXpcNkp17UJwKBgQCVrlyQBEXMJPBQWyS/pIDclBXsrgKzEHGrfFzQWdIJUfwXmRDPFqs2RRp2iQlilr3F2u+X75Ip2drKfDpNkxI/sFjeKRyfnH5XwlGhit9o2SEzWNoT2UrBOZccmXfBPVE5Ff4Kb5fAT4jges6U8qGqjlKWWVqutNlpIFgOIMFa3wKBgC+K9oDGYISOUeO99Bq4BXS8iyhoyQtqcM5pMTsQA0+dhTxACYOdwstIWXRi94t2qBE9Wr9h/8tnyhCF5PdhHHNerngE1ONxiDBxEBHMMhuKK1ENif795kOTvgR8tqWdfLYOuw8m4G2c2CKkl1x8JPHTcNpO+dDpnYhKQjhKQGGPAoGAJffEowNycQYtrzRvbe1cgpdNuGurY6CYNNem83LVRGf3ekTpbp5hb7K3s7mvF8MFAFs4y2GJwfPw+8vnPiZASseAw1nKWPJpWfmAm2mOohnxwgSXzNEhlUH2gtqq9emH4OMjPaJw6+dA4TBFlqK1P346lMi1p/tX6sK5k1vbsCECgYBPfs3VA1CYjeYdjxE4VTC5Uk3MvLY9D1YrxNKdi6FyVqqVKQrODdKQwcxMXWmhlR4oxcRx8EarDvc0//G29Fg/3Zm4G9l+SdUa2FYB6x+85vCmnyx+b1D2nX+OWaNl5vgyUb8nhqQbagBb28S6rPDZreB1wROolzbVi17EyKCCkg==";
    /**
     * 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
     */
    private final String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnkuT0BhWRcBwXqvwahmve/GFU8V+6hd3EerczO6wRwW7C6Iw9GNP/IdUwVKrJ/IYBFdlVgnMRCkPaRDdpPsXX+Tb45mMAIJ5Xn3PgNsjRPrdC0aPpTnOCgBqjTtpDruuxMQ7AnsBDb5d//GMf57em7oxTucQqtNrvu3308nah3eXML5Fb1T7mdhS6BsZyMOc12+KcvEkJVmBFkRdaghpPNVUFIZWrOU9f8mUdGyyl27MHaqOttmsgCToy582xDqk6RNSIwNrYnYVOXoCTgxaxo/SA8r0EujsUz5kgPQJBmyU3NYpTwQfEOqwKVw/W9TfTrhgNTolzh5x4zBzGOLUlQIDAQAB";
    /**
     * 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
     * TODO 内网穿透
     */
    private final String notifyUrl = "http://4qx3z9.natappfree.cc/payed/notify";

    /**
     * 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
     * 同步通知，支付成功，一般跳转到成功页
     */
    private final String returnUrl = "http://egomall.shop:8006/member/order";

    /**
     * 签名方式
     */
    private final String signType = "RSA2";

    /**
     * 字符编码格式
     */
    private final String charset = "utf-8";

    /**
     * 收单时间
     */
    private final String timeout = "30m";

    /**
     * 支付宝网关
     */
    private final String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                appId, merchantPrivateKey, "json",
                charset, alipayPublicKey, signType);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String outTradeNo = vo.getOutTradeNo();
        //付款金额，必填
        String totalAmount = vo.getTotalAmount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ outTradeNo +"\","
                + "\"total_amount\":\""+ totalAmount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        // 会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return alipayClient.pageExecute(alipayRequest).getBody();
    }
}
